package com.eascs.brand.dao;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;
import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.brand.entity.O2oPartnerEntity;
import com.eascs.brand.form.O2oBranPage;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;

@Repository
public class O2oPartnerDao extends BaseO2oDao{
	
	private static final String table_name="o2o_partner_info";
	public static final int status_delete=2;
	public static final int status_normal=1;
	
	public boolean insert(O2oPartnerEntity entity){
		entity.setCreateTime(new Date());
		entity.setStatus(status_normal);
		entity.setWfStatus(-1);
		
		int result=o2oJdbcSession.insert(entity);
		return result > 0 ? true : false;
	}
	
	public boolean delete(String partnerId){
		String sql="UPDATE " + table_name + " SET STATUS=? WHERE id=?";
		
		int result=o2oJdbcSession.execute(sql, new Object[]{status_delete,partnerId});
		return result > 0 ? true : false;
	}

	public boolean update(O2oPartnerEntity entity){
		entity.setUpdateTime(new Date());
		
		int result=o2oJdbcSession.updateById(entity);
		return result > 0 ? true : false;
	}
	
	public boolean processPartnerStart(String partnerId){
		String sql="UPDATE " + table_name + " SET wf_status=? WHERE id=?";
		
		int result=o2oJdbcSession.execute(sql, new Object[]{ProcessTaskMain.PROECESS_STATUS_HANDING,partnerId});
		return result > 0 ? true : false;
	}
	
	public boolean passPartnerStart(String partnerId){
		String sql="UPDATE " + table_name + " SET wf_status=? WHERE id=?";
		
		int result=o2oJdbcSession.execute(sql, new Object[]{ProcessTaskMain.PROECESS_STATUS_PASS,partnerId});
		return result > 0 ? true : false;
	}
	
	public boolean updatePartnerWfStatus(String partnerId,int wfStatus){
		String sql="UPDATE " + table_name + " SET wf_status=? WHERE id=?";
		
		int result=o2oJdbcSession.execute(sql, new Object[]{wfStatus,partnerId});
		return result > 0 ? true : false;
	}
	
	public O2oPartnerEntity queryPartnerEntityById(String partnerId){
		String sql="SELECT * FROM " + table_name + " WHERE id=?";
		
		return o2oJdbcSession.findEntity(sql, O2oPartnerEntity.class, partnerId);
	}
	
	public PageResult<Object[]> queryPartnerEntityByPage(O2oBranPage branForm) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, branForm.getPageSize(), branForm.getPageIndex());
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT p.id,p.partner_code,p.partner_name,p.create_time,p.par_person_liable,CONCAT( LEFT (p.par_mobile, 3), '****', RIGHT (p.par_mobile, 3)),p.is_partnership,p.loan_object,p.finance_money,p.wf_status ");
		sb.append(" FROM "+table_name);
		sb.append(" p WHERE p.brand_id=? and p.status=?");
		sb.append(" ORDER BY p.update_time DESC ");
		
		sqlPageQuery.addParam(branForm.getBrandId());
		sqlPageQuery.addParam(status_normal);
		sqlPageQuery.setQuerySql(sb.toString());
		return o2oJdbcSession.findDataWithPage(sqlPageQuery);
	}
	
	public List<Object[]> queryPartnerAllLikeName(String partnerName) {
		String sql = "SELECT id,partner_name FROM " + table_name + " WHERE  status = 1 AND wf_status in (0,9) AND partner_name LIKE ? LIMIT 10";
		
		return super.findEntitys(sql, Object[].class, "%" + partnerName + "%");
	}
	
	public List<Object[]> getPartnerIds(String partnerName, String brandName) {
		String sql = "SELECT opi.id FROM o2o_brand_info AS obr INNER JOIN o2o_partner_info AS opi ON obr.id = opi.brand_id WHERE obr.brand_name LIKE ? AND opi.partner_name LIKE ?";
		sql += " union SELECT opi.id FROM o2o_brand_info AS obr INNER JOIN o2o_dealer_partner_info AS opi ON obr.id = opi.brand_id WHERE obr.brand_name LIKE ? AND opi.business_license_name LIKE ?";
		return super.findEntitys(sql, Object[].class, "%" + brandName + "%", "%" + partnerName + "%", "%" + brandName + "%", "%" + partnerName + "%");
	}

	public List<Object[]> getPartnerIdsByName(String partnerName) {
		String sql = "SELECT opi.id FROM o2o_partner_info AS opi WHERE opi.partner_name LIKE ?";
		sql += " union SELECT opi.id FROM o2o_dealer_partner_info AS opi WHERE opi.business_license_name LIKE ?";
		return super.findEntitys(sql, Object[].class, "%" + partnerName + "%", "%" + partnerName + "%");
	}

	public List<Object[]> getPartnerIdsByBrandName(String brandName) {
		String sql = "SELECT opi.id FROM o2o_brand_info AS obr INNER JOIN o2o_partner_info AS opi ON obr.id = opi.brand_id WHERE obr.brand_name LIKE ?";
		sql += " union SELECT opi.id FROM o2o_brand_info AS obr INNER JOIN o2o_dealer_partner_info AS opi ON obr.id = opi.brand_id WHERE obr.brand_name LIKE ?";
		List<Object[]> data = super.findEntitys(sql, Object[].class, "%" + brandName + "%", "%" + brandName + "%");
		return data;
	}
}
